package com.example.bpmn.delegate;

import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;

@Slf4j
@Component
@RequiredArgsConstructor
public class TransportProcessDelegate implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) throws Exception {
        String taskDefinitionKey = execution.getCurrentActivityId();
        Double totalAmount = (Double) execution.getVariable("totalAmount");
        log.info("运输任务开始: taskDefinitionKey={}, totalAmount={}", taskDefinitionKey, totalAmount);
        
        switch (taskDefinitionKey) {
            case "Task_GenerateWaybill":
                generateWaybill(execution);
                break;
            case "Task_DispatchDriver":
                dispatchDriver(execution);
                break;
            case "Task_MonitorTransport":
                monitorTransport(execution);
                break;
            case "Task_ManualRouting":
                handleManualRouting(execution);
                break;
            case "Task_CentralDispatch":
                handleCentralDispatch(execution);
                break;
            default:
                log.warn("未知的任务类型: {}", taskDefinitionKey);
        }
    }

    private void generateWaybill(DelegateExecution execution) throws Exception {
        String businessKey = execution.getBusinessKey();
        log.info("开始生成运单, businessKey: {}", businessKey);
        
        @SuppressWarnings("unchecked")
        List<Map<String, Object>> mergedOrders = (List<Map<String, Object>>) 
            execution.getVariable("mergedOrders");
            
        // 生成运单信息
        Map<String, Object> waybill = new HashMap<>();
        waybill.put("waybillNo", "WB_" + businessKey);
        waybill.put("totalAmount", execution.getVariable("totalAmount"));
        waybill.put("orderCount", mergedOrders.size());
        
        // 生成站点信息
        generateCheckPoints(waybill, mergedOrders);
        
        execution.setVariable("waybill", waybill);
        log.info("运单生成完成: waybillNo={}", waybill.get("waybillNo"));
    }

    private void dispatchDriver(DelegateExecution execution) throws Exception {
        String businessKey = execution.getBusinessKey();
        log.info("开始派车, businessKey: {}", businessKey);
        
        String driverId = "DRIVER_" + System.currentTimeMillis();
        String vehicleNo = "车辆_" + System.currentTimeMillis();
        
        execution.setVariable("driverId", driverId);
        execution.setVariable("vehicleNo", vehicleNo);
        execution.setVariable("dispatchTime", System.currentTimeMillis());
        
        log.info("派车完成, 司机:{}, 车辆:{}", driverId, vehicleNo);
    }

    private void monitorTransport(DelegateExecution execution) throws Exception {
        String businessKey = execution.getBusinessKey();
        String waybillNo = (String) execution.getVariable("waybillNo");
        log.info("开始运输监控, businessKey: {}, waybillNo: {}", businessKey, waybillNo);
        
        execution.setVariable("currentLocation", "正在运输中");
        execution.setVariable("monitorTime", System.currentTimeMillis());
        
        log.info("运输监控更新完成");
    }

    private void generateCheckPoints(Map<String, Object> waybill, List<Map<String, Object>> orders) {
        List<Map<String, Object>> checkPoints = new ArrayList<>();
        
        // 添加取货点
        Map<String, Object> pickupPoint = new HashMap<>();
        pickupPoint.put("pointId", "P1");
        pickupPoint.put("address", "上海市浦东新区XX路XX号");
        pickupPoint.put("sequence", 1);
        pickupPoint.put("type", "PICKUP");
        checkPoints.add(pickupPoint);
        
        // 添加配送点
        for (int i = 0; i < orders.size(); i++) {
            Map<String, Object> order = orders.get(i);
            Map<String, Object> deliveryPoint = new HashMap<>();
            deliveryPoint.put("pointId", "D" + (i + 1));
            deliveryPoint.put("address", order.getOrDefault("address", "未知地址"));
            deliveryPoint.put("sequence", i + 2);
            deliveryPoint.put("type", "DELIVERY");
            deliveryPoint.put("orderId", order.get("orderId"));
            checkPoints.add(deliveryPoint);
        }
        
        waybill.put("checkPoints", checkPoints);
    }

    private void handleManualRouting(DelegateExecution execution) {
        String businessKey = execution.getBusinessKey();
        Double totalAmount = (Double) execution.getVariable("totalAmount");
        log.info("开始人工路由分配: businessKey={}, totalAmount={}", businessKey, totalAmount);
    }

    private void handleCentralDispatch(DelegateExecution execution) {
        String businessKey = execution.getBusinessKey();
        Double totalAmount = (Double) execution.getVariable("totalAmount");
        log.info("开始集中派车: businessKey={}, totalAmount={}", businessKey, totalAmount);
    }
} 